//
//  ZYNetworkTools.swift
//  WeiBo
//
//  Created by zhangyu on 16/9/22.
//  Copyright © 2016年 zhangyu. All rights reserved.
//

import UIKit
import AFNetworking
enum RequestType: Int {
    case GET
    case POST
}
class ZYNetworkTools: AFHTTPSessionManager {
    //单例全局访问点
    static let sharedTools:ZYNetworkTools = {
        let tools = ZYNetworkTools()
        //添加可以接受的响应数据类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    //封装get/post请求
    fileprivate func request(type: RequestType,url:String, params: Any?,callBack:@escaping (Any?,Error?)->()){
        if type == .GET{
//            get(url, parameters: params, progress: nil, success: { (_, response) in
//                callBack(response,nil)
//            }) { (_, error) in
//                callBack(nil,error)
//            }
            get(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response,nil)
                }, failure: { (_, error) in
                    callBack(nil,error)
            })
        }else{
            post(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response,nil)
                }, failure: { (_, error) in
                    callBack(nil,error)
            })
        }
        
    }
}
//  MARK: --微博首页相关接口
extension ZYNetworkTools{
    //  获取用户所关注的微博数据
    func requestStatuses(accessToken: String,maxId: Int64,sinceId: Int64,callBack:@escaping(Any?,Error?)->()){
        //准备接口地址
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
//        let params = ["access_token": accessToken]
        let params: [String: Any] = [
            "access_token": accessToken,
            "max_id": maxId,
            "since_id": sinceId
        ]
//        print(url + "?access_token=\(accessToken)")
        //执行接口请求
        request(type: .GET, url: url, params: params, callBack: callBack)
    }
}
//  MARK: --OAuth登录相关接口
extension ZYNetworkTools{
    func requestAccessToken(code:String,callBack:@escaping (Any?,Error?)->()){
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Uri
        ]
        request(type: .POST, url: url, params: params, callBack: callBack)
    }
    //  通过Accesstoken获取用户信息
    func requestUserInfo(accessToken: String, uid: Int64, callBack: @escaping (Any?, Error?) -> ()) -> Void {
        //  请求接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        //  准备请求需要的参数
        let params: [String: Any] = [
            "access_token": accessToken,
            "uid": uid
        ]
        
        request(type: .GET, url: url, params: params, callBack: callBack)
        
        
    }
}
